﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
		class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class=" ">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
				<el-col :span="24">
					<el-form-item label="检查水平" prop="inspectionLevel">
						<el-select v-model='dataForm.inspectionLevel' placeholder='请选择' clearable required
							:style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in inspectionLevelOptions" :key="index" :label="item.fullName"
								:value="item.enCode"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="质量限" prop="qualityLimit">
						<el-select v-model='dataForm.qualityLimit' placeholder='请选择' clearable required
							:style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in qualityLimitOptions" :key="index" :label="item.fullName"
								:value="item.enCode"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="严格度" prop="stage">
						<el-select v-model='dataForm.stage' placeholder='请选择' clearable required
							:style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in stageOptions" :key="index" :label="item.fullName"
								:value="item.enCode"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="是否全检" prop="allInspection">
						<el-radio-group v-model='dataForm.allInspection' required>
							<el-radio v-for="(item, index) in allInspectionOptions" :key="index" :label="item.id">{{
								item.fullName }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="批量下限" prop="lowerLimit">
						<el-input v-model='dataForm.lowerLimit' placeholder='请输入' clearable required
							:style='{ "width": "100%" }' maxlength='10'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="批量上限" prop="upperLimit">
						<el-input v-model='dataForm.upperLimit' placeholder='请输入' clearable required
							:style='{ "width": "100%" }' maxlength='10'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
						<el-form-item label="样本数量" prop="sampleQty">
							<el-input-number v-model='dataForm.sampleQty' placeholder='0' required :step='1' :min='0' >
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="接收数" prop="receivedQty">
							<el-input-number v-model='dataForm.receivedQty' placeholder='0' required :step='1' min='0' >
							</el-input-number>
						</el-form-item>
					</el-col>
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
		<!-- <dictionaryForm v-if="formVisible" ref="Form" @refreshDataList="getDictionaryData" /> -->
	</el-dialog>
</template>
<script>
import { getNationalstandardInfo, createNationalstandard, updateNationalstandard } from '@/api/q1/basedata/nationalstandard'
import dictionaryForm from '@/views/systemData/dictionary/Form'
export default {
	components: { dictionaryForm },
	props: [],
	data() {
		return {
			btnLoading: false,
			loading: false,
			visible: false,
			dataForm: {
				id: '',
				inspectionLevel: undefined,
				qualityLimit: undefined,
				stage: undefined,
				allInspection: undefined,
				lowerLimit: undefined,
				upperLimit: undefined,
				sampleQty: undefined,
				receivedQty: undefined,
			},
			rules: {
					inspectionLevel:[
						{
							required:true,
							message:'请输入检查水平',
							trigger:'"change"'
						},
					],
					qualityLimit:[
						{
							required:true,
							message:'请输入质量限',
							trigger:'"change"'
						},
					],
					stage:[
						{
							required:true,
							message:'请输入严格度',
							trigger:'"change"'
						},
					],
					allInspection:[
						{
							required:true,
							message:'请输入是否全检：',
							trigger:'"change"'
						},
					],
					lowerLimit:[
						{
							required:true,
							message:'请输入批量下限',
							trigger:'"blur"'
						},
						{
							pattern:/^\d+$|^\d*\.\d+$/g,
							message:'请输入正确的数字',
							trigger:'"blur"'
						},
					],
					upperLimit:[
						{
							required:true,
							message:'请输入批量上限',
							trigger:'"blur"'
						},
						{
							pattern:/^\d+$|^\d*\.\d+$/g,
							message:'请输入正确的数字',
							trigger:'"blur"'
						},
					],
					sampleQty:[
						{
							required:true,
							message:'请输入样本数量',
							trigger:["blur","change"]
						},
					],
					receivedQty:[
						{
							required:true,
							message:'请输入接收数',
							trigger:["blur","change"]
						},
					],
				},
			inspectionLevelOptions: [],
			qualityLimitOptions: [],
			stageOptions: [],
			allInspectionOptions: [{ 'fullName': "全检", 'id': "Y" }, { 'fullName': "不全检", 'id': "N" }],
		}
	},
	computed: {},
	watch: {},
	created() {
		this.getDictionaryData();
	},
	mounted() {
	},
	methods: {
		getDictionaryData() {
			this.$store.dispatch('base/getDictionaryData', { sort: 'gLevel' }).then((res) => {
				this.inspectionLevelOptions = res
			})
			this.$store.dispatch('base/getDictionaryData', { sort: 'gQualityLimit' }).then((res) => {
				this.qualityLimitOptions = res
			})
			this.$store.dispatch('base/getDictionaryData', { sort: 'gStringency' }).then((res) => {
				this.stageOptions = res
			})
		},
		goBack() {
			this.$emit('refresh')
		},
		init(id) {
			this.dataForm.id = id || 0;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['elForm'].resetFields();
				if (this.dataForm.id) {
					// request({
					// 	url: '/api/BaseData/QmsBaseNationalstandard/' + this.dataForm.id,
					// 	method: 'get'
					// }).then(res =>{
					// 	this.dataForm = res.data;
					// })
					getNationalstandardInfo(this.dataForm.id).then(res => {
						this.dataForm = res.data;
					})
				}
			});
			// this.$store.commit('generator/UPDATE_RELATION_DATA', {})
		},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					const formMethod = !this.dataForm.id ? createNationalstandard : updateNationalstandard;

					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
		},
	}
}
</script>
